package vn.vietel.pms.business;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;

import org.springframework.transaction.annotation.Transactional;

import vn.vietel.pms.entity.VKindOfProperty;
import vn.vietel.pms.service.KindOfPropertyService;

public class KindOfPropertyServiceImpl implements KindOfPropertyService {

	@PersistenceContext
	private EntityManager entityManager;

	@Transactional
	public void create(VKindOfProperty kind) {
		if (kind != null) {
			entityManager.persist(kind);
		}
	}

	@Transactional
	public VKindOfProperty find(Long id) {
		return entityManager.find(VKindOfProperty.class, id);
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	@Transactional
	public void delete(VKindOfProperty kind) {
		entityManager.remove(kind);

	}

	@Transactional
	public VKindOfProperty find(String kindName) {
		try {
			return (VKindOfProperty) entityManager
					.createQuery(
							"select g from VKindOfProperty g where g.kindname=:kindname")
					.setParameter("typename", kindName).getSingleResult();
		} catch (PersistenceException e) {
			return null;
		}
	}

	@Transactional
	public VKindOfProperty find(String kindName, String kindDesc) {
		try {
			return (VKindOfProperty) entityManager
					.createQuery(
							"select g from VKindOfProperty g where g.kindname=:kindname and g.kindDesc=:kindDesc")
					.setParameter("kindname", kindName).setParameter(
							"kindDesc", kindDesc).getSingleResult();
		} catch (PersistenceException e) {
			return null;
		}
	}

	@Transactional
	public void update(VKindOfProperty kind) {
		entityManager.merge(kind);

	}

	@SuppressWarnings("unchecked")
	public List<VKindOfProperty> getAll() {

		try {
			javax.persistence.Query qr = entityManager
					.createQuery("select g from VKindOfProperty g ");
			return qr.getResultList();

		} catch (PersistenceException e) {
			return null;
		}
	}

}
